//
// Driver.h
// Usage: Driver framework
// CreateDate: 2009-06-04
// Author: MaJiuyue
//
#ifndef _DRIVER_H_
#define _DRIVER_H_

#include <NewLifeDDK.h>
#include <RequestPackage.h>

class CDriver
{
public:
	CDriver();
	~CDriver();

	/* Process IRP */
	virtual bool ProcessMessage(RequestPackage *IRP, int * ret);

public:
	static CDriver * GetInstance();

	virtual int InitializeDriver() = 0;
	virtual int Shutdown() = 0;
	virtual void irq_handler(u16 IRQ) {}

protected:
	typedef bool (CDriver::*IRP_HANDLER)(RequestPackage *IRP, int *ret);
	IRP_HANDLER handler[IRP_COUNT];
	bool doDummy(RequestPackage *IRP, int *ret) { *ret=0; return true; }

protected:
	static u16 ports[];
	static u8 ports_nr;
	static u16 IRQs[];
	static u8 IRQs_nr;
	static bool bMapMemory;
	static u32 paddr;
	static u32 vaddr;
	static u32 size;

private:
	void InvalidMessage(struct message *pMsg);
	void RegisterPort();
	void RegisterIRQ();
	void UnRegisterPort();
	void UnRegisterIRQ();
	void MapMemory();
};

#define DECLARE_DRIVER(DRVNAME)	DRVNAME __drv;			\
CDriver * CDriver::GetInstance() { return (CDriver *)&__drv; }

#endif

